<template>
        <div class="login-box">
            <div class="form-login">
                <van-form @submit="submitForm" @failed="onFailed" ref="ruleFormRef" :model="form">
                    <div class="form-group">
                        <van-cell-group>
                            <van-field v-model="form.username" label="账号：" placeholder="请输入账号" clearable name="username"
                                :rules="usernameRules">
                            </van-field>
                        </van-cell-group>
                    </div>
                    <div class="form-group">
                        <van-cell-group>
                            <van-field v-model="form.password" label="密码：" placeholder="请输入密码" name="password" clearable
                                type="password" :rules="passwordRules">
                            </van-field>
                        </van-cell-group>
                    </div>
                    <div class="form-group">
                        <van-cell-group>
                            <van-button block round type="primary" native-type="submit">登录</van-button>
                        </van-cell-group>
                    </div>
                </van-form>
            </div>
        </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { login, getUser } from '../api'
import { useRouter } from 'vue-router'
import useToken from '../stores/token'
import useUser from '../stores/user'
const { updateToken } = useToken()
const { updateUser } = useUser()
const router = useRouter()
const form = reactive({
    username: 'demo1',
    password: '123456'
})
const ruleFormRef = ref()
const usernameRules = ref([
    { required: true, message: '用户名不能为空' }, { pattern: /^\w{3,16}$/, message: '用户名长度为 3-16 个字符 ' }])
const passwordRules = ref([
    { required: true, message: '密码不能为空' },
    { pattern: /^\w{6,24}$/, message: '密码必须为 6-24 位英文字母或数字 ' }])
const submitForm = async values => {
    const data = await login(values)
    if (data) {
        updateToken(data.token)
        const user = await getUser()
        updateUser({
            isLogin: true,
            username: user.username,
            avatar: user.avatar
        })
        router.push({ name: 'user' })
    }
}
const onFailed = errorInfo => {
    console.log('failed', errorInfo)
}
</script>
<style lang="less" scoped>
    .login-box {
        width: 400px;
        height: 250px;
        background-color: #f2f3f4;
        border-radius: 3px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        box-shadow: 0 0 6px rgba(194, 55, 55, 0.5);

        .form-login {
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            box-sizing: border-box;
            padding: 1.5rem !important;
            

            .form-group {
                padding-top: 20px;

                .btn {
                    background-color: rgb(0, 6, 7);
                    color: #fcfcfb;
                }
            }
        }
    }
</style>